/*
 * HatenaCategoryManagerApp.java
 */

package com.ryopei.hatena;

import org.apache.abdera.ext.wsse.WSSEAuthScheme;
import org.apache.abdera.protocol.Response.ResponseType;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
import org.apache.abdera.protocol.client.RequestOptions;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.swingx.JXLoginPane;
import org.jdesktop.swingx.JXLoginPane.Status;
import org.jdesktop.swingx.auth.LoginService;

/**
 * The main class of the application.
 */
public class HatenaCategoryManagerApp extends SingleFrameApplication {

    private AbderaClient abderaClient;

    private String userName;

    public AbderaClient getAbderaClient() {
        return abderaClient;
    }

    public String getUserName() {
        return userName;
    }


    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new HatenaCategoryManagerView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of HatenaCategoryManagerApp
     */
    public static HatenaCategoryManagerApp getApplication() {
        return Application.getInstance(HatenaCategoryManagerApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(HatenaCategoryManagerApp.class, args);
    }

    @Action
    public void connect() {
        Status status = JXLoginPane.showLoginDialog(this.getMainFrame(), new LoginService() {
            @Override
            public boolean authenticate(String userName, char[] password, String server) throws Exception {
                try {
                    String workspaceUri = "http://d.hatena.ne.jp/"+userName+"/atom/";
                    
                    AbderaClient client = new AbderaClient();
                    RequestOptions options = client.getDefaultRequestOptions();
                    options.setNoCache(true);
                    options.setUseLocalCache(false);
                    
                    WSSEAuthScheme.register(client, true);
                    client.addCredentials("http://d.hatena.ne.jp", null, "WSSE",
				new UsernamePasswordCredentials(userName, new String(password)));
                    ClientResponse res = client.get(workspaceUri);
                    if (res.getType() == ResponseType.SUCCESS) {
                        HatenaCategoryManagerApp.this.userName = userName;
                        HatenaCategoryManagerApp.this.abderaClient = client;

                        return true;
                    }
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
                return false;
            }
        });
    }
}
